//
//  JSRadioBoxCell.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/25.
//

import UIKit

class JSRadioBoxCell: JSBoxViewCell {
    var tempRadioItem :JSBoxItem = JSBoxItem()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        self.collectionView.snp.makeConstraints { (make) in
            make.right.equalTo(-self.cellMargin);
            make.centerY.equalTo(self.contentView.snp.centerY);
            make.height.equalTo(self.cellHeight);
        }
//        self.collectionView.backgroundColor = .red
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func initWith<T>(Model formModel: JSFormModel, VC ViewController: T) where T : BaseFormsController {
        super.initWith(Model: formModel, VC: ViewController)
        for item in self.model.items {
            if item.selected {
                self.tempRadioItem = item
                self.model.text = item.name
            }
        }
        
        if getCollectionViewWidth() > UIScreen.main.bounds.width - self.cellMargin*2 - 40 - 60
        {
            self.collectionView.snp.remakeConstraints { (make) in
                make.top.equalTo(self.iconBtn.snp.bottom).offset(0)
                make.left.equalTo(self.cellMargin)
                make.right.equalTo(-self.cellMargin)
                make.height.equalTo(getCollectionViewHeight())
            }
        }else
        {
            self.collectionView.snp.remakeConstraints { (make) in
//                make.left.equalTo(self.cellMargin)
                make.right.equalTo(-self.cellMargin)
                make.centerY.equalTo(self.contentView.snp.centerY)
                make.height.equalTo(self.cellHeight)
                make.width.equalTo(getCollectionViewWidth())
            }
        }

        
        self.collectionView.reloadData()
    }
    
    /// mark --获取FXRadioBoxCollectionView的宽度
    /// - Returns: 宽度
    func getCollectionViewWidth() -> CGFloat {
        let interitemSpacings:CGFloat = CGFloat((self.model.items.count - 1)) * self.layout.minimumInteritemSpacing
        var totalItemWidth:CGFloat = 0.1
        for item in self.model.items {
            totalItemWidth += item.width
        }
        return interitemSpacings + totalItemWidth
    }
    func getCollectionViewHeight() -> CGFloat {
        
        var row:Int = 1 // 行数
        let contentSizeWidth:Float = Float(UIScreen.main.bounds.width - self.cellMargin * 2 - 40) //collectionSize width 区域， 40： uicollectionView 的左右边距
        var itemWidths:Float = 0.0
        for item in self.model.items {
            
            itemWidths += Float(item.width + self.layout.minimumInteritemSpacing)
            if itemWidths > contentSizeWidth {
                itemWidths = Float(item.width+self.layout.minimumInteritemSpacing)
                row+=1
            }
        }

        let totalHeight:CGFloat = CGFloat(row) * kItemHeight + self.layout.sectionInset.top + self.layout.sectionInset.bottom
        self.model.height = totalHeight + self.cellHeight * 0.5 + self.iconBtnImageHeight * 0.5
        return totalHeight
        
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedItem = self.model.items[indexPath.item];
        if selectedItem.selected {
            return;
        }
        self.tempRadioItem.selected = false;
        selectedItem.selected = !selectedItem.selected
        self.tempRadioItem = selectedItem;
        self.collectionView.reloadData()
        
        self.model.text = selectedItem.name
//        self.parentVC.uploadDict[self.model.key!] =  selectedItem.name
//        self.model.items.filter({$0.name == selectedItem.name})
        let index = self.model.items.firstIndex(where: { $0.name == selectedItem.name})
        self.parentVC.uploadDict[self.model.key!] =  index! + 1
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
